﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>Gosub - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The Gosub statement jumps to the specified label and continues execution until a Return statement is encountered." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>Gosub</h1>

<p>Jumps to the specified label and continues execution until <a href="Return.htm">Return</a> is encountered.</p>

<pre class="Syntax"><span class="func">Gosub</span> Label
<span class="func">Gosub</span>("Label")</pre>
<h2 id="Parameters">参数</h2>
<dl>

  <dt>Label</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The name of the <a href="../misc/Labels.htm">label</a>, <a href="../Hotkeys.htm">hotkey label</a>, or <a href="../Hotstrings.htm#label">hotstring label</a> to which to jump, which causes the functions beneath <em>Label</em> to be executed until a Return or Exit is encountered. <a href="Return.htm">&quot;Return&quot;</a> causes the script to jump back to the first function beneath the Gosub and resume execution there. <a href="Exit.htm">&quot;Exit&quot;</a> terminates the <a href="../misc/Threads.htm">current thread</a>.</p>
  </dd>

</dl>

<h2 id="Remarks">备注</h2>
<p><em>Label</em> can be a variable or expression only if parentheses are used. 例如, <code>Gosub MySub</code> and <code>Gosub("MySub")</code> both execute the <code>MySub:</code> subroutine.</p>
<p>Performance is slightly reduced when using a dynamic label (that is, a variable or expression which returns a label name) because the target label must be "looked up" each time rather than only once when the script is first loaded. An error dialog will be displayed if the label does not exist. To avoid this, call <a href="IsLabel.htm">IsLabel</a> beforehand. 例如:</p>
<pre>if IsLabel(VarContainingLabelName)
    Gosub(VarContainingLabelName)</pre>
<p>Although Gosub is useful for simple, general purpose subroutines, consider using <a href="../Functions.htm">functions</a> for more complex purposes.</p>

<h2 id="Related">相关</h2>
<p><a href="Return.htm">Return</a>, <a href="../Functions.htm">Functions</a>, <a href="IsLabel.htm">IsLabel</a>, <a href="Block.htm">Blocks</a>, <a href="Loop.htm">Loop</a>, <a href="Goto.htm">Goto</a></p>

<h2 id="Examples">示例</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>Gosub Label1
MsgBox "The Label1 subroutine has returned (it is finished)."
return

Label1:
MsgBox "The Label1 subroutine is now running."
return</pre>
</div>

</body>
</html>